package com.dana.friend.controller.user;

import com.core.constants.HttpConstants;
import com.core.controller.BaseController;
import com.core.domain.Result;
import com.core.domain.vo.LoginUserVO;
import com.dana.friend.domain.user.dto.EditUserDTO;
import com.dana.friend.domain.user.dto.UserDTO;
import com.dana.friend.domain.user.vo.UserVO;
import com.dana.friend.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    /**
     * 生成验证码,并发送给用户
     */
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserDTO userSendCodeDTO){
        userService.sendCode(userSendCodeDTO);
        return Result.ok();
    }

    @PostMapping("/code/login")
    public Result<String> codeLogin(@RequestBody UserDTO userDTO){
        return Result.ok(userService.codeLogin(userDTO.getPhone(),userDTO.getCode()));
    }

    /**
     * 获取用户
     */
        @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        if (token==null){
            return Result.fail();
        }
        return userService.info(token);
    }



    /**
     * 用户退出登录
     */
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        if (token==null){
            return Result.fail();
        }
        boolean logout = userService.logout(token);
        return toResult(logout);
    }


    @GetMapping("/detail")
    public Result<UserVO> detail(){
      return Result.ok(userService.detail());
    }

    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody EditUserDTO editUserDTO){
       return toResult(userService.edit(editUserDTO));
    }

    @PutMapping("/head-image/update")
    public Result<Void> updateImage(@RequestBody EditUserDTO editUserDTO){
        return toResult(userService.updateImage(editUserDTO.getHeadImage()));
    }
}
